<template>
	<view style="padding: 30rpx;">
		<uni-forms ref="baseForm" :modelValue="form">
			<uni-forms-item label="姓名" required>
				<uni-easyinput v-model="form.username" placeholder="请输入姓名" />
			</uni-forms-item>
			<uni-forms-item label="电话" required>
				<uni-easyinput v-model="form.phone" placeholder="请输入电话号码" />
			</uni-forms-item>
			<uni-forms-item label="领取时间" required>
				<uni-datetime-picker disabled :border="false" type="date" placeholder="领取时间" v-model="form.createat"  />
			</uni-forms-item>
			<uni-forms-item label="试剂编号" required>
				<uni-easyinput v-model="form.deviceno" placeholder="请扫描设备二维码" />
			</uni-forms-item>
			<uni-forms-item label="备注" >
				<uni-easyinput type="textarea" v-model="form.reamark"  />
			</uni-forms-item>
			<button class="btn" @click="scanCode">扫一扫</button>
		</uni-forms>
		<button class="btn" style="margin-top: 50rpx;" @click="CreateClaimRecord()">提交</button>
	</view>
</template>

<script>
	import {
		CreateClaimRecord,FindLoginUser
	} from '@/utils/api.js'
	import store from '@/store';
	export default {
		data() {
			return {
				deviceNumber:'',
				form: {
					adminid:'',
					username: '',
					phone: '',
					createat:new Date().toISOString().split('T')[0],
					reamark:"",
					deviceno:'',
				}
			}
		},
		onLoad() {
			this.FindLoginUser()
		},
		methods: {
			async FindLoginUser() {
				const res = await FindLoginUser()
				console.log(res.data);
				this.form.adminid=res.data.userid
			},
			scanCode(){
				// 扫码
				let that =this
				uni.scanCode({
					scanType:['barCode','qrCode'],
					success: (res) => {
						const result = res.result;
						// 如果二维码里有http,则为二码合一设备，否则不是二码合一
						if(result.slice(0, 4)=='http'){
							const params = result.split('?')[1].split('&');
							that.deviceNumber = params[1].split('=')[1];
						}else{
							that.deviceNumber = result.split(',')[1];
						}
						console.log('扫码时的deviceNumber', that.deviceNumber);
						
						if (that.deviceNumber.substring(0, 3) == 'JNA') {
							that.form.deviceno=that.deviceNumber
						} else {
							uni.showToast({
								title: '请扫描配套产品二维码',
								icon: 'error'
							});
						}
					}
				});
			},
			CreateClaimRecord() {
				if(this.form.username==''||this.form.phone==''||this.form.deviceno==''){
					uni.showToast({
						icon: 'none',
						title: '请输入必填项',
						duration: 2000
					})
					return
				}
				uni.showModal({
					title: '领取',
					content: '您确定要领取该设备吗？',
					success: (res) => {
						if (res.confirm) {
							CreateClaimRecord(this.form).then(res => {
								console.log(res);
								if (res.code == 200) {
									uni.showToast({
										icon: 'success',
										title: '领取成功',
										duration: 2000
									})
									this.form = {
										adminid:'',
										username: '',
										phone: '',
										createat:new Date().toISOString().split('T')[0],
										reamark:"",
										deviceno:'',
									}
									// uni.navigateTo({
									// 	url: '/pages/consultation/index'
									// });
								}else{
									uni.showToast({
										icon: 'none',
										title: res.message,
										duration: 2000
									})
								}
							})
						} else if (res.cancel) {
							// 用户点击取消
							console.log('用户取消了领取');
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.btn {
		background-color: #30BAC3;
		color: white;
	}
	::v-deep .uni-forms-item__label{
		width: 150rpx !important;
	}
</style>